package com.cqeec.ui;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import com.cqeec.action.UserAction;
import com.cqeec.util.MD5;

public class EditPasswordFrame {
	static JInternalFrame editPasswordFrame = null;
	static JPasswordField old_password,new_password,confirm_password;
	static JButton btn_save;
	public EditPasswordFrame() {
	}

	public static void show(JDesktopPane deskPane) {
		if (editPasswordFrame == null) {
			editPasswordFrame = new JInternalFrame("修改密码", false, true, false, true);
			editPasswordFrame.setSize(360, 240);
			deskPane.add(editPasswordFrame);
			final Container c = editPasswordFrame.getContentPane();
			c.setLayout(null);
			JLabel lbl_old = new JLabel("旧密码：");
			c.add(lbl_old);
			lbl_old.setBounds(50,20,100,25);
			old_password = new JPasswordField();
			c.add(old_password);
			old_password.setEchoChar('#');
			old_password.setBounds(120, 20, 160, 25);
			
			JLabel lbl_new = new JLabel("新密码：");
			c.add(lbl_new);
			lbl_new.setBounds(50,70,100,25);
			new_password = new JPasswordField();
			c.add(new_password);
			new_password.setEchoChar('#');
			new_password.setBounds(120, 70, 160, 25);
			
			JLabel lbl_confirm = new JLabel("确认密码：");
			c.add(lbl_confirm);
			lbl_confirm.setBounds(50,120,100,25);
			confirm_password = new JPasswordField();
			c.add(confirm_password);
			confirm_password.setEchoChar('#');
			confirm_password.setBounds(120, 120, 160, 25);
			
			
			btn_save = new JButton("保存");
			c.add(btn_save);
			btn_save.setBounds(140, 167, 80, 25);
			btn_save.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					if (old_password.getText().trim().equals("")){
						JOptionPane.showMessageDialog(null, "请输入原始密码", "修改密码",JOptionPane.WARNING_MESSAGE); 
						old_password.requestFocusInWindow();
						return;
					}
					if (new_password.getText().trim().equals("")){
						JOptionPane.showMessageDialog(null, "请输入新密码", "修改密码",JOptionPane.WARNING_MESSAGE); 
						new_password.requestFocusInWindow();
						return;
					}
					if (confirm_password.getText().trim().equals("")){
						JOptionPane.showMessageDialog(null, "请输入确认密码", "修改密码",JOptionPane.WARNING_MESSAGE);
						confirm_password.requestFocusInWindow();
						return;
					}
					if (!confirm_password.getText().trim().equals(new_password.getText().trim())){
						JOptionPane.showMessageDialog(null, "新密码与确认密码不一致", "修改密码",JOptionPane.ERROR_MESSAGE);
						new_password.requestFocusInWindow();
						return;
					}
					if (!MD5.MD5Encode(old_password.getText().trim()).equals(MainFrame.userInfo.getUserpwd())){
						JOptionPane.showMessageDialog(null, "原始密码错误", "修改密码",JOptionPane.ERROR_MESSAGE);
						old_password.requestFocusInWindow();
						return;
					}
					UserAction action = new UserAction();
					if (action.EditPassword(MD5.MD5Encode(new_password.getText().trim()), MainFrame.userInfo.getUserid())){
						JOptionPane.showMessageDialog(null, "操作成功", "修改密码",JOptionPane.INFORMATION_MESSAGE);
						MainFrame.userInfo.setUserpwd(MD5.MD5Encode(new_password.getText().trim()));
						old_password.setText("");
						new_password.setText("");
						confirm_password.setText("");
					}else{
						JOptionPane.showMessageDialog(null, "操作失败，请与系统管理员联系", "修改密码",JOptionPane.ERROR_MESSAGE);
					}				
				}
			});
			editPasswordFrame.setVisible(true);
		}
		editPasswordFrame.addInternalFrameListener(new InternalFrameAdapter() {
			public void internalFrameClosing(InternalFrameEvent e) {
				old_password = null;
				new_password = null;
				confirm_password = null;
				btn_save = null;
				editPasswordFrame = null;
			}

		});
	}
}
